package com.gowhere.service.impl.basedata;

import com.github.pagehelper.PageHelper;
import com.gowhere.domain.Orders;
import com.gowhere.domain.Product;
import com.gowhere.domain.Traveller;
import com.gowhere.mapper.basedata.OrdersMapper;
import com.gowhere.service.basedata.OrdersService;
import com.gowhere.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public List<Orders> findByPage(int pageNum, int pageSize) {
        SqlSession session = MapperUtil.getSession();
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> list = mapper.findAll();
        for (Orders orders : list) {
            Date orderTime = orders.getOrderTime();
            SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String timestr = simpleDateFormat.format(orderTime);
            orders.setOrderTimeStr(timestr);
        }
        System.out.println(list);
        MapperUtil.close(session);
        return list;
    }

    @Override
    public void deleteById(String ids) {
        SqlSession session = MapperUtil.getSession();
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);
        String[] split = ids.split(",");
        for (String id : split) {
            //System.out.println(id);
            mapper.deleteById(id);
        }

        MapperUtil.close(session);
    }

    @Override
    public List<Orders> findAllDetails(String oid) {
        SqlSession session = MapperUtil.getSession();
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);
        List<Orders> orders = mapper.findAllDetails(oid);
        //处理时间格式放入Str
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Orders order : orders) {


            Date orderTime = order.getOrderTime();
            if (orderTime != null) {
                String timestr = simpleDateFormat.format(orderTime);
                order.setOrderTimeStr(timestr);
            }

            //处理证件字段类型
            List<Traveller> travellers = order.getTravellers();
            for (Traveller traveller : travellers) {
                Integer credentialsType = traveller.getCredentialsType();
                if (credentialsType != null) {
                    if (credentialsType == 0) {
                        traveller.setCredentialsTypeStr("身份证");
                    } else {
                        traveller.setCredentialsTypeStr("学生证");
                    }
                }
            }


            //处理支付方式
            Integer payType = order.getPayType();
            if (payType != null) {
                if (payType == 0) {
                    order.setPayTypeStr("在线支付");
                } else {
                    order.setPayTypeStr("线下支付");
                }
            }

            //
            Product product = order.getProduct();
            if (product != null) {
                Date departureTime = product.getDepartureTime();
                if (departureTime != null) {
                    String dt = simpleDateFormat.format(departureTime);
                    product.setDepartureTimeStr(dt);
                }
            }
        }

        MapperUtil.close(session);
        return orders;
    }

    @Override
    public List<Orders> findByLike(String likekeywords) {
        SqlSession session = MapperUtil.getSession();
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);

        List<Orders>ordersList= mapper.findByLike(likekeywords);




        for (Orders orders : ordersList) {
            String id = orders.getId();
            //System.out.println("id:"+id);

            SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date orderTime = orders.getOrderTime();
            if(orderTime!=null){
                String timestr = simpleDateFormat.format(orderTime);
                orders.setOrderTimeStr(timestr);
            }

        }



        MapperUtil.close(session);
        return ordersList;
    }

    @Override
    public void updateStatus0ByIds(String ids) {
        SqlSession session = MapperUtil.getSession();
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);
        String[] split = ids.split(",");
        Orders o=null;
        //根据id查询到orders 对象,判断orders对象中的status属性是1 or 0,取反并更新数据库
        for (String id : split) {
            //System.out.println(id);

            mapper.updateStatus0ByIds(id);

        }

        MapperUtil.close(session);

    }

    @Override
    public void updateStatus1ByIds(String ids) {
        SqlSession session = MapperUtil.getSession();
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);
        String[] split = ids.split(",");
        Orders o=null;
        //根据id查询到orders 对象,判断orders对象中的status属性是1 or 0,取反并更新数据库
        for (String id : split) {
            //System.out.println(id);

            mapper.updateStatus1ByIds(id);

        }

        MapperUtil.close(session);

    }


//    @Override
//    public Orders findById(String pid) {
//        SqlSession session = MapperUtil.getSession();
//        OrdersMapper mapper = session.getMapper(OrdersMapper.class);
//        return null;
//    }
}
